package org.example.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.commonapi.domain.CourseClass;
import org.example.commonapi.domain.R;
import org.example.commonapi.domain.StudentClass;
import org.example.commonapi.dto.StudentDto;
import org.example.commonapi.mapper.CourseClassMapper;
import org.example.commonapi.mapper.StudentClassMapper;
import org.example.course.service.StudentClassService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author XieNoDesire
* @description 针对表【student_class】的数据库操作Service实现
* @createDate 2024-06-02 17:34:49
*/
@Service
public class StudentClassServiceImpl extends ServiceImpl<StudentClassMapper, StudentClass>
    implements StudentClassService {

    @Resource
    private StudentClassMapper studentClassMapper;

    @Resource
    private CourseClassMapper courseClassMapper;
    /**
     * 邀请学生
     * @param studentClass
     */
    @Override
    public void inviteStudent(List<StudentClass> studentClass) {
        int number = studentClass.size();
        for(StudentClass stu : studentClass){
            studentClassMapper.insert(stu);
        }
//        获取到该班级原来的成员数
        CourseClass courseClass=courseClassMapper.selectById(studentClass.get(0).getClassId());
//        更新班级人数
        courseClass.setNumber(courseClass.getNumber()+number);
        courseClassMapper.updateById(courseClass);
    }

    /**
     * 修改分班
     * @param studentClass
     */
    @Override
    public void updateStudentClass(StudentClass studentClass) {
        studentClassMapper.updateStudentClass(studentClass.getStudentId(),studentClass.getClassId());
    }

    /**
     * 查询班级内学生
     * @param classId
     * @param username
     * @param no
     * @return
     */
    @Override
    public List<StudentDto> listStudent(Long classId, String username, String no) {
        return studentClassMapper.listStudent(classId,username,no);
    }

    /**
     * 加入班级
     * @param invite
     * @param studentClass
     */
    @Override
    public boolean joinStudentClass(String invite, StudentClass studentClass) {
        LambdaQueryWrapper<CourseClass> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseClass::getId, studentClass.getClassId());
        CourseClass courseClass = courseClassMapper.selectOne(queryWrapper);
        return courseClass.getInvite().equals(invite);
    }
}




